<?php

namespace app\utils\Service\Tencent;

use addons\wanlshop\library\EasyWeChat\Easywechat;
use app\utils\CurlUtil;
use EasyWeChat\Factory;

class MiniProgram extends Module
{
    private $app;

    public function __construct()
    {
        $this->app = Easywechat::app('applet');
    }

    private function getToken($app)
    {
        return $app->access_token->getToken()['access_token'];
    }

    /**
     * 查询订单发货状态
     * @param $transaction_id
     * @return bool
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function wechatSyncCheck($transaction_id)
    {
        $app    = $this->app;

        $data = [
            'transaction_id' => $transaction_id,
        ];

        $token = $this->getToken($app);

        $result_content = $app->http_client->post("/wxa/sec/order/get_order?access_token={$token}", [
            'body'   => json_encode($data, JSON_UNESCAPED_UNICODE),
        ])->getBody()->getContents();

        $result = json_decode($result_content, true);

        if (!isset($result['errcode']) || $result['errcode'] != 0) {
            return $this->error('操作失败',$result);
        }

        return $this->success('获取成功',$result);
    }
}